﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using ViCode_LeVi.Configs;
using ViCode_LeVi.Data;
using System.Windows.Media;
using Xceed.Wpf.Toolkit;

namespace ViCode_LeVi.Controls
{
    /// <summary>
    /// <toolkitXceed:RichTextBox  SpellCheck.IsEnabled="True" SpellCheck.SpellingReform="PreAndPostreform" x:Name="_richTextBox" Grid.Row="1" Margin="10" BorderBrush="Gray" Padding="10"
            //                              Text="{Binding Notes}" 
            //                              ScrollViewer.VerticalScrollBarVisibility="Auto" >
            //    <toolkitXceed:RichTextBox.TextFormatter>
            //        <toolkitXceed:RtfFormatter />
            //    </toolkitXceed:RichTextBox.TextFormatter>
            //    <toolkitXceed:RichTextBoxFormatBarManager.FormatBar>
            //        <toolkitXceed:RichTextBoxFormatBar />
            //    </toolkitXceed:RichTextBoxFormatBarManager.FormatBar>
            //</toolkitXceed:RichTextBox>
    /// </summary>
    public class VMRichTextBox : Xceed.Wpf.Toolkit.RichTextBox
    {
        public VMRichTextBox()
        {
            this.TextFormatter = new RtfFormatter();
            RichTextBoxFormatBarManager.SetFormatBar(this, new RichTextBoxFormatBar());

            SpellCheck.SetIsEnabled(this, true);
            SpellCheck.SetSpellingReform(this, SpellingReform.PreAndPostreform);

            FontFamily_Read();

            var fontFamily = _FontFamily_Default;
            if (fontFamily != null)
                this.FontFamily = fontFamily;

            var fontSize = _FontSize_Default;
            if (fontSize > 0)
            {
                this.FontSize = fontSize;                
            }
        }
        public static double _FontSize_Default = 0;
        public static FontFamily _FontFamily_Default = null;
        public static void FontFamily_Read()
        {
            if (_FontFamily_Default == null)
            {
                var style = Application.Current.Resources[typeof(TextBox)] as Style;
                if (style != null)
                {
                    var setter_FontFamily = style.Setters.FirstOrDefault(c => ((Setter)c).Property == TextBox.FontFamilyProperty);
                    if (setter_FontFamily != null)
                        _FontFamily_Default = (FontFamily)(((Setter)setter_FontFamily).Value);

                    //var setter_FontSize = style.Setters.FirstOrDefault(c => ((Setter)c).Property == TextBox.FontSizeProperty);
                    //if (setter_FontSize != null)
                    //    _FontSize_Default = (double)(((Setter)setter_FontSize).Value);

                    if (Application.Current.Resources.Contains("FES_MainFontSize"))
                    {
                        _FontSize_Default = (double)Application.Current.Resources["FES_MainFontSize"];
                    }
                }
            }            
        }
    }
    public class VMTextBox : TextBox
    {
        public VMTextBox()
            : base()
        {
            if (Style == null)
            {
                this.Style = Application.Current.Resources[typeof(TextBox)] as Style;
            }
            //try
            //{
            //    this.FontFamily = new System.Windows.Media.FontFamily( VMConfig.Intance.UIFont.TextBox_FontName);
            //    this.FontSize = VMConfig.Intance.UIFont.TextBox_FontSize;
            //}
            //catch { }            
            SetupFormatInfo();
        }
        public void SetupFormatInfo()
        {
            VMTextBoxFormatInfo.Intance.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(Intance_PropertyChanged);
            VMTextBoxFormatInfo.Intance.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Intance_PropertyChanged);
            OverridesDefaultStyle = true;            
            Intance_PropertyChanged(null, null);
        }
        public void RefreshFormat()
        {
            Intance_PropertyChanged(null, null);
        }
        void Intance_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            var format = VMTextBoxFormatInfo.Intance;
            if (format != null)
            {
                //this.Background = Brushes.White;
                this.Background = format.Background;
                this.Foreground = format.ForeColor;
                if (!string.IsNullOrEmpty(format.FontText))
                {
                    this.FontFamily = new System.Windows.Media.FontFamily(format.FontText);
                }
                this.FontSize = format.dFontSize;
                if (format.IsBold == true)
                    this.FontWeight = FontWeights.Bold;
                else
                    this.FontWeight = FontWeights.Normal;
                if (format.IsItalic == true)
                    this.FontStyle = FontStyles.Italic;
                else
                    this.FontStyle = FontStyles.Normal;

                if (format.IsUnder)
                {
                    // Create an underline text decoration. Default is underline.
                    TextDecoration myUnderline = new TextDecoration();
                    // Create a linear gradient pen for the text decoration.
                    Pen myPen = new Pen();
                    myPen.Brush = format.ForeColor;
                    myPen.Brush.Opacity = 1;
                    myPen.Thickness = 1;
                    myPen.DashStyle = DashStyles.Solid;
                    myUnderline.Pen = myPen;
                    myUnderline.PenThicknessUnit = TextDecorationUnit.FontRecommended;
                    // Set the underline decoration to a TextDecorationCollection and add it to the text block.
                    TextDecorationCollection myCollection = new TextDecorationCollection();
                    myCollection.Add(myUnderline);
                    this.TextDecorations = myCollection;
                }
                else
                {
                    this.TextDecorations = null;
                }
                if (format.IsCheckSpell == true)
                {
                    SpellCheck.SetIsEnabled(this, true);
                }
                else
                {
                    SpellCheck.SetIsEnabled(this, false);
                }
            }
        }
    }
    public class VMTextBox_Number : VMTextBox
    {
        public VMTextBox_Number()
            : base()
        {
            if (Style == null)
            {
                this.Style = Application.Current.Resources[typeof(TextBox)] as Style;
            }
            this.TextAlignment = TextAlignment.Right;
            this.GotFocus += new RoutedEventHandler(VMTextBox_Number_GotFocus);
            SetupFormatInfo();
        }

        void VMTextBox_Number_GotFocus(object sender, RoutedEventArgs e)
        {
            this.SelectAll();
        }
    }
    public class VMTextBox_Number_ReadOnly : VMTextBox_Number
    {
        public VMTextBox_Number_ReadOnly()
            : base()
        {
            if (Style == null)
            {
                this.Style = Application.Current.Resources[typeof(TextBox)] as Style;
            }
            this.TextAlignment = TextAlignment.Right;
            this.IsReadOnly = true;
            this.IsTabStop = false;
            this.VerticalAlignment = VerticalAlignment.Top;
            SetupFormatInfo();
        }
    }
    public class VMTextBox_Number_ReadOnly_Finance_Trade : VMTextBox_Number_ReadOnly
    {
        public VMTextBox_Number_ReadOnly_Finance_Trade()
        {
            this.Margin = new Thickness(2, 2, 2, 10);
            SetupFormatInfo();
        }
    }
    public class VMTextBox_Answer : VMRichTextBox//VMTextBox_MultiLine
    {
        public VMTextBox_Answer()
            : base()
        {
            //if (Style == null)
            //{
            //    this.Style = Application.Current.Resources[typeof(TextBox)] as Style;
            //}
            //TextWrapping = TextWrapping.WrapWithOverflow;
            //SetupFormatInfo();
        }
    }
    public class VMTextBox_MultiLine : VMTextBox
    {
        public VMTextBox_MultiLine()
            : base()
        {
            if (Style == null)
            {
                this.Style = Application.Current.Resources[typeof(TextBox)] as Style;
            }
            this.AcceptsReturn = true;
            //this.AcceptsTab = true;
            TextWrapping = TextWrapping.Wrap;
            ScrollViewer.SetVerticalScrollBarVisibility(this, ScrollBarVisibility.Auto);
            SetupFormatInfo();
        }
    }
}
